package cn.ktp.auth.service;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fescar.core.context.RootContext;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

import cn.ktp.auth.dao.UserDao;
import cn.ktp.auth.entity.User;
import cn.ktp.auth.iservice.IAuthService;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class AuthService implements IAuthService {
	
	@Autowired
	UserDao userDao;

	@HystrixCommand(commandProperties = {
			@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000") })
	@Override
	public String hello() {
		int nextInt = new Random().nextInt(4000);
		System.out.println("sleep " + nextInt + "ms");
		try {
			Thread.sleep(nextInt);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return "HELLO  " + nextInt + " from Dubbo Spring Boot";
	}

	@Override
	public User addUser(User user) {
		System.out.println("全局事务id ：" + RootContext.getXID());
		userDao.insert(user);
		return user;
	}

	@Override
	public List<User> listUser() {
		return userDao.selectUsers();
	}

	
}
